library Article15 initializer init requires LegacySystem


	//PV 5 - CE/clays/misc
	//PV 10 - Marine units, pets
	//PV 11 - Aegis .50 Cal Turret
	//PV 13 - Civilians, trucks, misc.

	globals
		private trigger Art15Trigger
	endglobals
	
	function SetArticle15Enabled takes boolean b returns nothing
		if b then
			call EnableTrigger(Art15Trigger)
		else
			call DisableTrigger(Art15Trigger)
		endif
	endfunction
	
	private function Article15Conditions takes nothing returns boolean
		// if killer is ally or killed is ally then art 15/killed trigger is invoked
		return IsUnitAlly(GetKillingUnit(),Player(0)) or IsUnitAlly(GetTriggerUnit(),Player(0)) 
	endfunction
	
	function GiveArticle15 takes player p, real expfactor, string msg returns nothing
		set udg_XPFactor=udg_XPFactor*expfactor
		if not udg_Article15[GetConvertedPlayerId(p)] then
			set udg_Article15[GetConvertedPlayerId(p)] = true
			call ALICEText(5.0, msg + " Article 15 invoked.")
		else
			call ALICEText(5.0, msg)
		endif
	endfunction
	
	private function Article15 takes nothing returns nothing
		local unit u = GetTriggerUnit() //Killed unit
		local unit u2 = GetKillingUnit() //Killer
		local player victim = GetOwningPlayer(u)
		local player p = GetOwningPlayer(u2)
		local integer pv = GetUnitPointValue(u)
		local integer article = 0
		local integer id = GetUnitTypeId(u2)

		if GetPlayerController(p)==MAP_CONTROL_USER and victim!=p then
			set article=1
		endif
		
		if GetUnitTypeId(u)=='h00E' then
			if victim==p then
				call DisplayTextToPlayer(GetLocalPlayer(),0.0,0.0, "MQ-3 Predator has destroyed itself." )
			elseif GetPlayerController(p)==MAP_CONTROL_USER then
				call DisplayTextToPlayer(GetLocalPlayer(),0.0,0.0, "MQ-3 Predator has been destroyed by friendly fire from " + GetPlayerName(p) )
			else
				call DisplayTextToPlayer(GetLocalPlayer(),0.0,0.0, "MQ-3 Predator has been destroyed." )
			endif
			call putBoolean(null,"UAVPresent",false)
		endif
		
		if IsUnitType(u, UNIT_TYPE_HERO) then
			set article = 0
			if victim==p then
				call DisplayTextToPlayer(GetLocalPlayer(),0.0,0.0, GetPlayerName(p) + " has killed himself." )
			elseif GetPlayerController(p)==MAP_CONTROL_USER or GetUnitTypeId(u2)=='n00S' then
				if udg_Article15[GetConvertedPlayerId(p)] then
					call DisplayTextToPlayer( GetLocalPlayer(), 0., 0., GetPlayerName(victim) + " has been killed by " + GetPlayerName(p) + "." )
					set udg_XPFactor=udg_XPFactor*0.8
					//set udg_XPFactor = udg_XPFactor - 0.05
				else
					set udg_Article15[GetConvertedPlayerId(p)] = true
					set udg_XPFactor=udg_XPFactor*0.9
					//set udg_XPFactor = udg_XPFactor - 0.03
					call DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, GetPlayerName(victim) + " has been killed by " + GetPlayerName(p) + ". Article 15 invoked." )
				endif
			else
				call DisplayTextToPlayer( GetLocalPlayer(), 0.0, 0.0, GetPlayerName(victim) + " has been killed." )
			endif
		endif
		
		if article==1 and u!=udg_Boss and pv!=5 and pv!=11 and u!=u2 then
			if udg_Article15[GetConvertedPlayerId(p)] then
				set udg_XPFactor=udg_XPFactor*0.8
				if IsUnitType(u,UNIT_TYPE_MECHANICAL)==true then
					call DisplayTextToPlayer( GetLocalPlayer(), 0, 0, GetUnitName(u) + " has been destroyed by " + GetPlayerName(p) + "." )
				else
					call DisplayTextToPlayer( GetLocalPlayer(), 0, 0, GetUnitName(u) + " has been killed by " + GetPlayerName(p) + "." )
				endif
			else
				set udg_Article15[GetConvertedPlayerId(p)] = true
				set udg_XPFactor=udg_XPFactor*0.9
				if IsUnitType(u,UNIT_TYPE_MECHANICAL)==true then
					call ALICEText(5.0, GetUnitName(u) + " has been destroyed by " + GetPlayerName(p) + ". Article 15 invoked." )
				else
					call ALICEText(5.0, GetUnitName(u) + " has been killed by " + GetPlayerName(p) + ". Article 15 invoked." )
				endif
			endif
			if udg_Mode==2 and udg_Bunny[1]!=1 then
				set udg_Bunny[1]=1
				call GeneralText(15.0, "|cfff3ad00Major Grimes|r : You get Article 15s for fatally wounding friendly forces. An Article 15 will stop you from advancing any medals in regular games and reduce your entire squad's XP gain." )
				call PolledWait(10.)
				call GeneralText(15.0, "|cfff3ad00Major Grimes|r : Use explosives a safe distance away from friendlies and watch each other's back. An Article 15 will ruin your career, Marine!" )
			endif
		endif
		set u = null
		set u2 = null
		set p = null
		set victim = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterPlayerUnitEvent(t, Player(0), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(1), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(2), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(3), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(4), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(5), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(6), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(7), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(8), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(9), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(10), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerRegisterPlayerUnitEvent(t, Player(11), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerAddCondition(t, Condition(function Article15Conditions))
		call TriggerAddAction(t,function Article15)
		set Art15Trigger = t
	endfunction


endlibrary